package com.nanhang.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanhang.entity.Classes;
import com.nanhang.entity.Student;
import com.nanhang.mapper.StudentMapper;
import com.nanhang.service.IClassesService;
import com.nanhang.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanhang.util.PageResult;
import com.nanhang.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.sql.Wrapper;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cwy
 * @since 2025-01-07
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private IClassesService classesService;
    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {
        Page<Student> page1 = this.page(new Page<>(page,pageSize));
        return new PageResult<>(page1.getRecords(), page1.getTotal());
    }

    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVO studentVO) {
        System.out.println("classesService");
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(studentVO.getSname()),"sname",studentVO.getSname());
        queryWrapper.like(StrUtil.isNotBlank(studentVO.getAddr()),"addr",studentVO.getAddr());
        queryWrapper.eq(studentVO.getCid()!=null && studentVO.getCid() != 0,"cid",studentVO.getCid());
        Page<Student> page1 = this.page(new Page<>(page,pageSize), queryWrapper);
        page1.getRecords().forEach(st -> {
            Classes classes = classesService.getById(st.getCid());
            st.setCname(classes.getCname());
        });
        System.out.println("classesService");
        return new PageResult<>(page1.getRecords(), page1.getTotal());
    }
}
